import { createRouter, createWebHistory } from 'vue-router'
import Layout from '../components/Layout/Layout.vue'
import Login from '../pages/Login.vue'
import Dashboard from '../pages/Dashboard.vue'
import NotFound from '../pages/NotFound.vue'
import menus from './menus'

function filterRoutes (menus) {
  const arr = []

  function loop (list) {
    for (const i in list) {
      const item = list[i]
      if (item.component) {
        arr.push({
          path: item.path,
          component: item.component,
          meta: {
            label: item.label,
            ...item.meta,
            id: item.id
          }
        })
      }

      if (item.children) {
        loop(item.children)
      }
    }
  }

  loop(menus)

  return arr
}

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      component: Layout,
      children: [
        { path: '/', component: Dashboard },
        ...filterRoutes(menus),
        { path: '/:pathMatch(.*)*', component: NotFound }
      ]
    },
    { path: '/login', component: Login },
    { path: '/:pathMatch(.*)*', component: NotFound }
  ]
})

export default router
